package com.jfsnpm.controller;


import org.apache.commons.lang.StringUtils;

import com.jfinal.core.Controller;
import com.jfsnpm.action.PmMenu;
import com.jfsnpm.action.PmOrg;
import com.jfsnpm.common.AppSql;
import com.jfsnpm.common.AppTool;
import com.jfsnpm.common.MenuTree4Auth;
import com.jfsnpm.plugin.bjui.Bjui;
import com.jfsnpm.plugin.bjui.BjuiRender;


public class SystemController extends Controller {
	/**
	 * 菜单管理
	 */
	public void menu(){
		setAttr("menulist", PmMenu.getAllMenu());
	}
	public void menu_save(){
		// 删除菜单
		String menudeleteids = getPara("deleteids");
		String[] deleteidlist = AppTool.json2Object(menudeleteids,String[].class);
		// 保存菜单
		String menus = getPara("menus");
		
		if(PmMenu.deleteMenus(deleteidlist)&&AppTool.json2MenuTree(menus).saveTree()){
			render(BjuiRender.success("保存成功！",false));
		}else{
			render(BjuiRender.error("保存失败！"));
		}
	}
	public void menu_getrole(){
		String menuid = getPara("id");
		if(StringUtils.isBlank(menuid)){render(BjuiRender.code(801));return;}
		Bjui.BjuiPage(AppSql.getsql("pm.PmRelationMenuRoleSelectP"), AppSql.getsql("pm.PmRelationMenuRoleFromWhereP"),
				"", "", this, menuid);
		setAttr("menuid", menuid);
	}
	
	
	/**
	 * 角色管理
	 */
	public void role(){
		Bjui.BjuiPageByTableName("pm_role", "id,name", this);
	}
	public void role_save() {
		if(Bjui.updateTable("pm_role", this)){
			render(BjuiRender.success("保存成功！",false));
		}else{
			render(BjuiRender.error("保存失败！"));
		}
	}
	public void role_getmenu(){
		String roleid = getPara("roleid","");
		setAttr("menulist", PmMenu.getAllMenuWithAuth(roleid));
		setAttr("roleid", roleid);
	}
	public void role_menu_save(){
		String menus = getPara("menus");
		String roleid = getPara("roleid");
		MenuTree4Auth tree = AppTool.json2MenuTree4Auth(menus);
		if(tree.saveTree4Auth(roleid)){
			render(BjuiRender.success("保存成功！",false));
		}else{
			render(BjuiRender.error("保存失败！"));
		}
	}
	public void role_getuser(){
		String roleid = getPara("roleid","");
		setAttr("menulist", PmOrg.getAllOrgWithAuth(roleid));
		setAttr("roleid", roleid);
	}
	public void role_user_save(){
		String menus = getPara("menus");
		String roleid = getPara("roleid");
		MenuTree4Auth tree = AppTool.json2MenuTree4Auth(menus);
		if(tree.saveTree4Auth4Org(roleid)){
			render(BjuiRender.success("保存成功！",false));
		}else{
			render(BjuiRender.error("保存失败！"));
		}
	}
	/**
	 * 用户管理
	 */
	public void user(){
		//Bjui.BjuiPageByTableName("pm_user", "no,name", this);
		Bjui.BjuiPage("select * from pm_user where id <> 'admin'", "no,name", this);
	}
	public void user_save() {
		if(Bjui.updateTable("pm_user", this)){
			render(BjuiRender.success("保存成功！",false));
		}else{
			render(BjuiRender.error("保存失败！"));
		}
	}
	/**
	 * 组织架构
	 */
	public void org(){
		setAttr("menulist", PmOrg.getAllOrg());
	}
	public void org_save(){
		// 删除
		String menudeleteids = getPara("deleteids");
		String[] deleteidlist = AppTool.json2Object(menudeleteids,String[].class);
		// 保存
		String menus = getPara("menus");
		
		if(PmOrg.deleteOrgs(deleteidlist)&&AppTool.json2OrgTree(menus).saveTree()){
			render(BjuiRender.success("保存成功！",false));
		}else{
			render(BjuiRender.error("保存失败！"));
		}
	}
	
	
	
}
